package com.ycq.springboot.jdbc.contorller;


import com.ycq.springboot.jdbc.dao.JpaUserRepository;
import com.ycq.springboot.jdbc.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/jpa")
public class JpaController {
    //注入JPA接口，这里不需要实现类
    @Autowired
    private JpaUserRepository jpaUserRepository=null;

    @RequestMapping("/getUser")
    public User getUser(Long id){
        //使用jpa接口查询对象
        User user=jpaUserRepository.findById(id).get();
        return user;
    }

    @RequestMapping("/getUserById")
    public User getUserById(Long id){
        //使用JPA接口查询对象
        User user=jpaUserRepository.getUserById(id);
        return user;
    }

    @RequestMapping("/findByUsernameLike")
    public List<User> findByUsernameLike(String userName){
        //使用jpa接口查询对象
        List<User> userList=jpaUserRepository.findByUsernameLike("%"+userName+"%");
        return  userList;
    }

    @RequestMapping("/findByUserNameLikeOrNoteLike")
    public List<User> findByUserNameLikeOrNoteLike(String userName,String note){
        String userNameLike="%"+userName+"%";
        String noteLike="%"+note+"%";
        //使用JPA接口查询对象
        List<User> userList=jpaUserRepository.findByUsernameLikeOrNoteLike(userNameLike,noteLike);
        return  userList;
    }

    @RequestMapping("/getUser1")
    @ResponseBody
    public String getUser1(){
        //使用jpa接口查询对象
       // User user=jpaUserRepository.findById(id).get();
        return "haha";


    }
}
